//
//  IOOperation.m
//  PrivateCloud
//
//  Created by Visitor on 10/13/13.
//  Copyright (c) 2013 sarah doo. All rights reserved.
//

#import "IOOperation.h"
#import "AppUtil.h"
#import "DDFileInfo.h"
#import "DDConstants.h"
@implementation IOOperation
{
    // 写入文件对象
	NSFileHandle *_fileHandle;
}

- (long)getFileLength:(NSString *)fileName{
    
    NSString *type = [DDConstants getFileInfoType:fileName];
    if(type != nil){
        NSString* filePath = [NSString stringWithFormat:@"%@%@/%@",[AppUtil getDocPath],type,fileName];
        NSFileManager * filemanager = [[NSFileManager alloc]init];
        if([filemanager fileExistsAtPath:filePath]){
            NSDictionary * attributes = [filemanager attributesOfItemAtPath:filePath error:nil];
            NSNumber *theFileSize = [attributes objectForKey:NSFileSize];
            return [theFileSize longValue];
        }
        [filemanager release];
    }
    return -1;
}

- (long)getVcardFileLength:(NSString *)fileName{
    NSFileManager * filemanager = [[NSFileManager alloc]init];
    if([filemanager fileExistsAtPath:fileName]){
        NSDictionary * attributes = [filemanager attributesOfItemAtPath:fileName error:nil];
        NSNumber *theFileSize = [attributes objectForKey:NSFileSize];
        [filemanager release];
        return [theFileSize longValue];
        
    }
    return -1;
}


- (BOOL)writeData:(NSData *)data andOffset:(long)off andUID:(int)uid{
    
    NSTimeInterval timea=[[NSDate date] timeIntervalSince1970]*1000;
    double aa = timea;      //NSTimeInterval返回的是double类型
    NSLog(@"off-------------%ld,%d",off,[data length]);
    NSFileManager* fileManager = [NSFileManager defaultManager];
    DDFileInfo *fileInfo = [DDConstants getServerFileInfoById:uid];
    //获取文件的类型
    NSString * type = [DDConstants getFileInfoType:[fileInfo fileName]];
    BOOL result = NO;
    if (type != nil) {
        NSString* filePath = [NSString stringWithFormat:@"%@%@/%@",[AppUtil getDocPath],type,[fileInfo fileName]];
        if(![fileManager fileExistsAtPath:filePath]){
            [fileManager createFileAtPath:filePath contents:nil attributes:nil];
        }
        _fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
        [_fileHandle seekToFileOffset:off];
        [_fileHandle writeData:data];
        [_fileHandle writeData:data];
        [_fileHandle closeFile];
//        [fileData release];
        result = YES;
        
    }
    [fileManager release];
    return NO;
}

- (BOOL)writeData:(NSData *)data andOffset:(long)off andPath:(NSString*)path{
    NSFileManager* fileManager = [NSFileManager defaultManager];
    //获取文件的类型
    BOOL result = NO;
    NSString* filePath = path;
//    if([fileManager fileExistsAtPath:filePath]){
//        [fileManager removeItemAtPath:filePath error:nil];
//        
//    }
    //[fileManager createFileAtPath:filePath contents:nil attributes:nil];
    _fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    [_fileHandle seekToFileOffset:off];
//    NSMutableData* fileData = [[NSMutableData alloc]initWithContentsOfFile:filePath];
//    [fileData appendData:data];
    [_fileHandle writeData:data];
    [_fileHandle closeFile];
//    [fileData release];
    result = YES;
    
    [fileManager release];
    return NO;
}

- (NSData*)readDataByOffset:(long)off andLength:(int)len andUID:(int)uid{
    NSFileManager* fileManager = [NSFileManager defaultManager];
    DDFileInfo *fileInfo = [DDConstants getLocalFileInfoById:uid];
    //获取文件的类型
    NSString * type = [DDConstants getFileInfoType:[fileInfo fileName]];
    NSString* filePath = [NSString stringWithFormat:@"%@%@/%@",[AppUtil getDocPath],type,[fileInfo fileName]];
    NSData *data = nil;
    if([fileManager fileExistsAtPath:filePath]){
        _fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
        [_fileHandle seekToFileOffset:off];
        data = [_fileHandle readDataOfLength:len];
    }
    
    [fileManager release];
    return data;
}

- (NSData*)readDataByOffset:(long)off andLength:(int)len andPath:(NSString *)path{
    NSFileManager* fileManager = [NSFileManager defaultManager];
    //    DDFileInfo *fileInfo = [DDConstants getLocalFileInfoById:uid];
    //获取文件的类型
    //    NSString * type = [DDConstants getFileInfoType:[fileInfo fileName]];
    //    NSString* filePath = [NSString stringWithFormat:@"%@%@/%@",[AppUtil getDocPath],type,[fileInfo fileName]];
    NSData *data = nil;
    if([fileManager fileExistsAtPath:path]){
        NSMutableData* fileData = [[NSMutableData alloc] initWithContentsOfFile:path];
        NSRange range;
        range.location = off;
        range.length = len;
        data = [fileData subdataWithRange:range];
    }
    return data;
}
//
//- (NSData *)readData:(DDFileInfo *)
//{
//    NSMutableData *mData = [[NSMutableData alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@%@",[AppUtil getDocPath],fileData.fileID]];
//    Byte *text = (Byte *)[mData bytes];
//    text += text[fileData.filerange];
//    NSData *data = [[NSData alloc] initWithBytes:text length:fileData.fileLenth];
//    return data;
//}
@end
